﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

/*********************************************************
 * 命名空间 MDT.VirtualSoftPlatform.PlantEntity
 * 接口名称 PageList
 * 开发人员：-nhy
 * 创建时间：2022/6/27 14:11:58
 * 描述说明：
 * 更改历史：
 * 
 * *******************************************************/
namespace Simple.Tools
{
    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class SImplePageList<T> : List<T>
    {
        /// <summary>
        /// 
        /// </summary>
        public int CurrentPage { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int TotalPages { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int PageSize { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int TotalCount { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public bool HasPrevious => CurrentPage > 1;
        /// <summary>
        /// 
        /// </summary>
        public bool HasNext => CurrentPage < TotalPages;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="items"></param>
        /// <param name="count"></param>
        /// <param name="pageNumber"></param>
        /// <param name="pageSize"></param>

        public SImplePageList(List<T> items, int count, int pageNumber, int pageSize)
        {

            PageSize = pageSize;
            TotalCount = count;
            CurrentPage = pageNumber;
            TotalPages = count < 0 ? 0 : (int)Math.Ceiling(count / (double)pageSize);
            if (items != null)
            {
                AddRange(items);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="item"></param>
        public SImplePageList(T item)
        {
            if (item != null)
            {
                Add(item);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="source"></param>
        /// <param name="Params"></param>
        /// <returns></returns>
        public static SImplePageList<T> Create(List<T> source, BaseParameter Params)
        {
            var count = source == null ? 0 : source.Count;

            var items = source == null ? null : source.Skip(Params.PageSize * (Params.PageIndex - 1)).Take(Params.PageSize).ToList();

            return new SImplePageList<T>(items, count, Params.PageIndex, Params.PageSize);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="source"></param>
        /// <param name="Params"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static SImplePageList<T> Create(T[] source, BaseParameter Params,int count)
        {
            return new SImplePageList<T>(source.ToList(),count,Params.PageIndex, Params.PageSize);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="source"></param>
        /// <param name="Params"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static SImplePageList<T> Create(List<T> source,BaseParameter Params,int count)
        {
            return new SImplePageList<T>(source,count,Params.PageIndex,Params.PageSize);
        }
    }
}
